package com.example.es02.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.es02.DO.SnnDist;
import com.example.es02.DO.SnnDistExample;
import com.example.es02.bean.Dist;
import com.example.es02.dao.SnnDistMapper;

import com.example.es02.util.ESUtils;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;


import org.elasticsearch.client.Requests;
import org.elasticsearch.client.RestHighLevelClient;


import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TestService {
    @Autowired
    private SnnDistMapper snnDistMapper;

    @Autowired
    private ESUtils esUtilsl;
//    @Autowired
//    @Qualifier("Client")
//    private RestHighLevelClient client;
//    @Autowired
//    private ElasticsearchRestTemplate restTemplate;
//    public String addAll(){
//            restTemplate.createIndex(Dist.class);
//            restTemplate.putMapping(Dist.class);
//            List<SnnDist> snnDists = snnDistMapper.selectByExample(new SnnDistExample());
//            BulkRequest request = new BulkRequest();
//            snnDists.forEach(e->{
//                request.add(new UpdateRequest("snn_dist",String.valueOf(e.getId()))
//                        .doc(BeanUtil.beanToMap(
//                                new Dist(String.valueOf(e.getId()),e.getDistCode(),e.getSysOrgCode(),e.getDistName(),e.getCreditCode(),String.valueOf(e.getDeleteFlag())
//                                )))
//                        .docAsUpsert(true));
//            });
//            BulkResponse bulk = null;
//            try {
//                bulk = client.bulk(request, RequestOptions.DEFAULT);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            boolean b = bulk.hasFailures();
//            if(b){
//                return "fail";
//            }else
//                return "success";
//        }

    public String newaddAll(){
        List<Object> maps = snnDistMapper.selectAllMap();
        BulkResponse bulkItemResponses = esUtilsl.initIndexAndPushDate(new Dist(), maps);
        if(bulkItemResponses!=null&&!bulkItemResponses.hasFailures()){
            return "success";
        }else {
            return "fail";
        }
    }

    public String  upnew(String id){
        Dist dist=new Dist();
        dist.setId(id);
        dist.setDistName("ceshi");
        Boolean aBoolean = esUtilsl.updateORinsert(id, dist);
        if(aBoolean){
            return "success";
        }else {
            return "fail";
        }
    }

    public List<Map<String,Object>> select(String key,String indexName,Integer size,Integer page){
       return esUtilsl.selectEs(key, indexName, size, page);
    }

    public String de(String index,String[] key){
        BulkResponse bulkItemResponses = esUtilsl.deleteES(index, key);
        if(bulkItemResponses!=null&&!bulkItemResponses.hasFailures()){
            return "success";
        }else {
            return "fail";
        }
    }

//    public String update(String id,String index,Object c){
//        UpdateQuery updateQuery = new UpdateQuery();
//        updateQuery.setClazz(c.getClass());
//        updateQuery.setId(id);
//        updateQuery.setIndexName(index);
//        updateQuery.setDoUpsert(true);
//        updateQuery.setUpdateRequest(new UpdateRequest("index", id).doc(BeanUtil.beanToMap(c)));
//        UpdateResponse update = restTemplate.update(updateQuery);
//        int successful = update.getShardInfo().getSuccessful();
//        if(successful>=0){
//            return "success";
//        }else
//            return "fail";
//
//
//    }
//
//    public List se(String key){
//        List list=new ArrayList();
//        SearchRequest request = new SearchRequest();
//        // 构建查询的请求体
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(QueryBuilders.fuzzyQuery("dist_name",key).fuzziness(Fuzziness.ONE));
//        sourceBuilder.query(QueryBuilders.matchQuery("dist_name",key));
//        sourceBuilder.size(500);
//        sourceBuilder.from(0);
//        request.source(sourceBuilder);
//        try {
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            SearchHits hits = response.getHits();
//            for (SearchHit hit : hits) {
////输出每条查询的结果信息
//                list.add(hit.getSourceAsMap());
//            }
//            return list;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return new ArrayList();
//    }
}
